package com.app.micaihu.view.home.questions.presenter

import com.app.base.mvp.BasePresenter
import com.app.http.NetRequestListener
import com.app.http.bean.BaseBean
import com.app.micaihu.bean.infor.UploadFileResult
import com.app.micaihu.bean.topic.RecommendTopicList
import com.app.micaihu.configure.LoadType
import com.app.micaihu.configure.TypeConstant
import com.app.micaihu.manager.UserInforManager
import com.app.micaihu.utils.ApiServiceUtils.apiService
import com.app.micaihu.utils.OkHttp3Helper
import com.app.micaihu.utils.ParamsMapUtils
import com.app.micaihu.view.bean.QuestionList
import com.app.micaihu.view.home.questions.contract.QuestionsContract
import com.app.utils.biz.Biz
import java.io.File
import java.util.*

/**
 * @author
 * @date 2020
 */
open class QuestionsPresenter : BasePresenter<QuestionsContract.View>(), QuestionsContract.Presenter {

    private var isRefresh = true
    private var page = 1
    private var mark = 0
    private var firstLoad = true
    private var lastGetTime = ""

    override fun questionList() {
        if (firstLoad) {
            getView()?.showLoading()
            firstLoad = false
        }
        val params = ParamsMapUtils.commonParams
        params["page"] = page
        params["type"] = 3
        params["mark"] = mark
        params["lastGetTime"] = lastGetTime
        if (UserInforManager.getInstance().isLogin) {
            params["uid"] = UserInforManager.getInstance().userInfor.uid
        }
        requestNetwork(apiService.cmtList(params), object : NetRequestListener<BaseBean<QuestionList?>?> {
            override fun onSuccess(result: BaseBean<QuestionList?>?) {
                result?.let { data ->
                    data.data?.let {
                        lastGetTime = it.lastGetTime
                    }
                }
                getView()?.hideLoading()
                page++
                val loadType = if (isRefresh) LoadType.TYPE_REFRESH_SUCCESS else LoadType.TYPE_LOAD_MORE_SUCCESS
                getView()?.handleQuestionList(result, loadType)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.hideLoading()
                val loadType = if (isRefresh) LoadType.TYPE_REFRESH_ERROR else LoadType.TYPE_LOAD_MORE_ERROR
                getView()?.handleQuestionList(null, loadType)
                getView()?.onError(e)
            }
        })
    }

    override fun refresh() {
        page = 1
        isRefresh = true
        questionList()
    }

    override fun loadMore() {
        isRefresh = false
        questionList()
    }

    override fun upFile(file: File) {
        val requestBodyParams: HashMap<String?, String?> = HashMap()
        requestBodyParams.put("type", TypeConstant.UPLOAD_TYPE2)
        requestBodyParams.put("uid", Biz.instance.uid())
        requestBodyParams.put("filename", "filename")
        requestBodyParams.put("timestamp", System.currentTimeMillis().toString())

        val parameterName = OkHttp3Helper.UploadMD5Encryption(requestBodyParams, file)

        requestNetwork(apiService.uploadImg(parameterName), object : NetRequestListener<BaseBean<UploadFileResult?>?> {
            override fun onSuccess(result: BaseBean<UploadFileResult?>?) {
                getView()?.hideLoading()
                getView()?.handleUploadFile(result)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.hideLoading()
                getView()?.handleUploadFile(null)
                getView()?.onError(e)
            }
        })
    }


}